package com.chookss.push;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.chookss.HomeActivityss;
import com.chookss.Urls;
import com.chookss.WebCommonActivity;
import com.chookss.base.StaticClass;
import com.chookss.base.Token;
import com.chookss.home.NavigationBaseActivity;
import com.chookss.home.audit.clockIn.ClockInSureActivity;
import com.chookss.home.interview.InterviewRecordsActivity;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.johnrambo.ktea.ktExtends.ShareUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class PushUtils {
    public static void getPushData(Context context) {
        Intent intent = null;
        String type = "", code = "", addressType = "", pushMessageId = "";
        int id = 2;
        try {
            JSONObject jsonObject = new JSONObject(new ShareUtils().getString(context, StaticClass.PUSH_DATA, ""));
            Log.d("MyMessageReceiver", jsonObject.toString());
//            type类型：1、公告 2、新闻3、考试4、h5页面 5、面试待评价列表
            if (jsonObject.has("type")) {
                type = jsonObject.getString("type");
            }
            if (jsonObject.has("code")) {
                code = jsonObject.getString("code");
            }
            if (jsonObject.has("pushMessageId")) {
                pushMessageId = jsonObject.getString("pushMessageId");
            }
            if ("1".equals(type)) {
                if (Utils.isNull(code)) {
                    intent = new Intent(context, NavigationBaseActivity.class);
                    intent.putExtra("type", 1);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                } else {
                        intent = new Intent(context, WebCommonActivity.class);
                        if (Urls.baseUrl.contains("dev_kmer")) {
                            addressType = "1";
                        } else {
                            addressType = "0";
                        }
                        String url = Urls.webBaseUrl + "indexNotice.html#/noticeDetail?noticeCode=" + code + "&token=" + Token.INSTANCE.getToken() + "&statusHeight=" + Utils.getStatusBarHeightDp(context) + "&time=" + System.currentTimeMillis() + "&addressType=" + addressType;
                        intent.putExtra("url", url);
                        intent.putExtra("waterMark", "12");
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                }
                context.startActivity(intent);
            } else if ("2".equals(type)) {
                if (!Utils.isNull(code)) {
                    intent = new Intent(context, WebCommonActivity.class);
                    if (Urls.baseUrl.contains("dev_kmer")) {
                        addressType = "1";
                    } else {
                        addressType = "0";
                    }
                    String url = Urls.webBaseUrl + "indexNew.html#/newDetail?newsCode=" + code + "&token=" + Token.INSTANCE.getToken() + "&statusHeight=" + Utils.getStatusBarHeightDp(context) + "&time=" + System.currentTimeMillis() + "&addressType=" + addressType;
                    intent.putExtra("url", url);
                    intent.putExtra("waterMark", "13");
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                }
            } else if ("3".equals(type)) {
                intent = new Intent(context, NavigationBaseActivity.class);
                intent.putExtra("type", 5);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            } else if ("4".equals(type)) {
                if (!Utils.isNull(code)) {
                    intent = new Intent(context, WebCommonActivity.class);
                    intent.putExtra("title", "详情");
                    intent.putExtra("url", code);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                }

            } else if ("5".equals(type)) {
                intent = new Intent(context, InterviewRecordsActivity.class);
                intent.putExtra("type", 2);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }else if ("6".equals(type)) {
                if (Utils.isNull(code)) {
                    intent = new Intent(context, HomeActivityss.class);
                } else {
                    if(code.length()>11){
                        intent = new Intent(context, ClockInSureActivity.class);
                        String time=code.substring(6,12);
                        intent.putExtra("time",time.substring(0,4)+"-"+ time.substring(4,6));
                        intent.putExtra("noticeCode", code);
                    }else{
                        intent = new Intent(context, HomeActivityss.class);
                    }
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                }
            }
            new ShareUtils().putString(context, StaticClass.PUSH_DATA, "");
            updatePushRead(pushMessageId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public static void updatePushRead(String pushMessageId) {
        if (Utils.isNull(pushMessageId)) {
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("pushMessageId", pushMessageId);
        MyHttpRequest.getDefault().getRequestCompat(null, Urls.updatePushRead, SignUtils2.makeSignStr(map), false, new ObserverCallback<String>() {
            @Override
            public void onSuccess(String data) {
            }

            @Override
            public void onError(String errorMsg) {
            }
        });
    }
}
